<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layuimini/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuimini/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <blockquote class="layui-elem-quote">
            <button class="layui-btn layui-btn-sm" id="pushSet">开始执行推送</button>
            <span class="layui-word-aux layui-form-mid" style="margin-left: 220px;float:right"></span>
        </blockquote>
        <table id="robotList" lay-filter="robotList"></table>
    </div>
</div>
<script src="/static/layuimini/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form','layer','laydate','table','laytpl'],function(){
        var layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            table = layui.table

        console.log(parent.updateData);
        console.log(parent.updateType);
        console.log(parent.updateGruop);
        console.log(parent.updateName)

        //软件列表
        table.render({
            elem: '#robotList',
            url : "{:url('data/getRobotList')}",
            cellMinWidth : 95,
            height : "full-100",
            id : "robotList",
            page:false,
            cols : [[
                {type:'checkbox',LAY_CHECKED:true},
                {type:'numbers'},
                {field: 'wx_img', title: '头像', width:80,align:"center",templet:function(d){
                        return '<img src="'+ d.wx_img+'" height="30px">';
                    }},
                {field: 'mp_user', title: '机器人微信ID', width: 170},
                {field: 'mp_name', title: '名称', width: 230},
                {field: 'state',title: '推送状态', minWidth:100,align:"center",templet:function(d){
                        return '<div id="status_'+ d.mp_user+'"><span class="layui-badge">等待推送</span></div>'
                    }},
            ]]
        });

        $('#pushSet').click(function(e){
            $('.layui-form-mid').html('开始推送中...........');
            var countSuccess = 0;
            var countError = 0;
            var robotList = getCheckData('robotList');
            if(robotList.length < 1){ layer.alert('请选中要推送的机器人');return; }

            for(var i=0;i<robotList.length;i++){
                //var url = parent.updateName == 'huiFu' ? 'huiFu/data.php':'data.php';

                $.ajax({
                    url:"{:url('manage')}",
                    type:'POST',
                    dataType: 'json',
                    async:true,
                    data:{'typeName':'updateConfigure','configreName':parent.updateName,'robotId':robotList[i]['mp_user'],data:parent.updateData},
                    success:function(d){
                        var status = '';
                        if(d.code == 1){
                            countSuccess = countSuccess+1;
                            status = '<span class="layui-badge layui-bg-green">推送成功</span>';
                        }else {
                            countError = countError+1
                            status = '<span class="layui-badge layui-bg-black">推送失败</span>';
                        }
                        $(d.statusId).html(status)
                        if(countSuccess + countError == robotList.length){
                            $('.layui-form-mid').html('推送完成');
                            layer.alert('<span class="layui-badge layui-bg-green">推送成功</span> <span class="layui-badge-rim">'+countSuccess+'</span><br /><span class="layui-badge layui-bg-black">推送失败</span> <span class="layui-badge-rim">'+countError+'</span>');
                        }
                    }
                })
            }
        })

        function getCheckData(tableId){
            //获取行ID
            var checkStatus = table.checkStatus(tableId),data = checkStatus.data;
            return data;
        }

    })
</script>


</body>
</html>